//
//  SurroundViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "SurroundViewController.h"

#import "CustomSalTableCell.h"
#import "SaleModel.h"
#import "ConnectModel.h"
#import "SurroundDetailViewController.h"
@interface SurroundViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate, UINavigationBarDelegate>

@end

@implementation SurroundViewController
- (void)dealloc
{
    [_tableArray release];
    [_salTable release];
    [_indexPath release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor yellowColor]];
    [self.navigationItem setTitle:@"团队旅游"];
    //置顶
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//    [self.view addSubview:view];
    //[self.navigationController.navigationBar addSubview:view];
    //[view setBackgroundColor:[UIColor yellowColor]];
    
   // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    //tap.numberOfTapsRequired = 1;
//   [self.navigationController.navigationBar addGestureRecognizer:tap];
    //[view addGestureRecognizer:tap];
   // tap.delegate = self;
    //[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(tap:)];
    //[tap release];
    //[view release];
    
    
    [self createTable];
    [self connect];
}
- (void)createTable
{
    self.salTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    [_salTable setDelegate:self];
    [_salTable setDataSource:self];
    [_salTable setRowHeight:350];
    [self.view addSubview:_salTable];
    [_salTable release];
}
#pragma mark -
#pragma mark - 轻拍手势置顶事件
- (void)tap:(UITapGestureRecognizer *)tap
{
    [_salTable scrollToRowAtIndexPath:self.indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    NSLog(@"aaa");
}

#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"salPool";
    CustomSalTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[CustomSalTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    
    SaleModel *model = [_tableArray objectAtIndex:indexPath.row];
    cell.model = model;
    //[model release];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SurroundDetailViewController *detail = [[SurroundDetailViewController alloc] init];
    SaleModel *model = [_tableArray objectAtIndex:indexPath.row];
    [detail getID:model.pk];
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark -
#pragma mark 网络请求
- (void)connect
{
    NSURL *url = [NSURL URLWithString:@"http://breadtrip.com/tp/products/?category=team_travel&dest=%E5%9B%BD%E5%86%85&page=1"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *array = [dic objectForKey:@"products"];
        for (NSDictionary *d in array) {
            SaleModel *model = [[SaleModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [self.tableArray addObject:model];
            [model release];
        }
        [_salTable reloadData];
    }];



}

































@end
